package com.jamaskii.dictation;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.jamaskii.ecdict.WordBookStream;

import java.io.File;

public class MainActivity extends BaseActivity {
    public Handler hand;
    private Thread thread;
    private long startTime;
    private int count=0;
    public static MainActivity instance;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hand=new Handler();
        instance=this;
        requestPermissions();
    }

    private void startUp(){
        startTime=System.currentTimeMillis();
        if(!isAccept()){
            Intent intent=new Intent();
            intent.setClass(MainActivity.this,GuideActivity.class);
            startActivityForResult(intent,1);
        }else{
            loadHome();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //此处可以根据两个Code进行判断，本页面和结果页面跳过来的值
        if (requestCode == 1 && resultCode==RESULT_OK) {
            boolean accept=data.getBooleanExtra("accept",false);
            if(accept){
                File file=new File(getExternalFilesDir(null)+"/accept");
                try{
                    file.createNewFile();
                }catch (Exception e){ }
                loadHome();
            }else{
                finish();
            }
        }
    }

    private void loadHome(){
        final Intent intent=new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(MainActivity.this,HomeActivity.class);
        long costTime=System.currentTimeMillis()-startTime;
        final long wait=1000-costTime;
        thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    if(wait>0){
                        Thread.sleep(wait);
                    }
                    hand.post(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(intent);
                        }
                    });
                }catch (Exception e){
                    System.exit(0);
                }
            }
        });
        thread.start();
    }

    private void requestPermissions() {
        // 如果未获得外部存储读写权限，则申请
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }else{
            count++;
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET},1);
        }else{
            count++;
        }
        if(count==2){
            startUp();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            // 拒绝权限
            System.exit(1);
        }else{
            count++;
            if(count==2){
                startUp();
            }
        }
    }

    private boolean isAccept(){
        File file=new File(getExternalFilesDir(null)+"/accept");
        return file.exists();
    }


}